package com.soushangxing.comfyuibackendv2.controller

import com.soushangxing.comfyuibackendv2.bean.GlobalQueue
import com.soushangxing.comfyuibackendv2.bean.ResponseType
import com.soushangxing.comfyuibackendv2.service.ServerService
import com.soushangxing.comfyuibackendv2.service.UserService
import jakarta.servlet.http.HttpServletRequest
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import java.util.*

@CrossOrigin
@Controller
@ResponseBody
@RequestMapping("server")
class ServerController {

    @Autowired
    lateinit var userService:UserService
    @Autowired
    lateinit var serverService: ServerService
    @Autowired
    lateinit var globalQueue: GlobalQueue

    /**
     * 添加一个服务器
     */
    @PostMapping("add")
    fun addServer(url:String, taskLimit:Int,
                           name:String?, description:String?,
                       request: HttpServletRequest
    ): ResponseType {
        if (!userService.isAdmin(request)){
            return ResponseType(null,2,"非管理员禁止调用本接口")
        }
        return serverService.addServer(url, taskLimit, name, description)
    }


    /**
     * 启动一个服务器
     */
    @PostMapping("start")
    fun startServer(url:String?,
                          request: HttpServletRequest
    ): ResponseType {
        if (!userService.isAdmin(request)){
            return ResponseType(null,2,"非管理员禁止调用本接口")
        }
        if (!url.isNullOrEmpty()){
            return serverService.startServerByUrl(url)
        } else {
            return ResponseType("请填入url")
        }
    }

    /**
     * 关闭一个服务器
     */
    @PostMapping("stop")
    fun stopServer(url:String?,
                    request: HttpServletRequest
    ): ResponseType {
        if (!userService.isAdmin(request)){
            return ResponseType(null,2,"非管理员禁止调用本接口")
        }
        if (!url.isNullOrEmpty()){
            return serverService.stopServerByUrl(url)
        } else {
            return ResponseType("请填入url")
        }
    }


    /**
     * 查看服务器列表
     */
    @GetMapping("list")
    fun getServerList(
                  request: HttpServletRequest
    ): ResponseType {
        if (!userService.isAdmin(request)){
            return ResponseType(null,2,"非管理员禁止调用本接口")
        }
        return serverService.getServerList()
    }
    /**
     * 查看服务器列表（后端变量）
     */
    @GetMapping("backend-list")
    fun getServerBackendList(
        request: HttpServletRequest
    ): ResponseType {
        if (!userService.isAdmin(request)){
            return ResponseType(null,2,"非管理员禁止调用本接口")
        }
        return ResponseType(globalQueue.globalServer,0,"");
    }

    /**
     * 更改服务器任务数
     */
    @PostMapping("change-task-limit")
    fun changeTaskLimit(
        url:String, taskLimit: Int,
        request: HttpServletRequest
    ): ResponseType {
        if (!userService.isAdmin(request)){
            return ResponseType(null,2,"非管理员禁止调用本接口")
        }
        return serverService.changeServerTaskLimitByUrl(url,taskLimit)
    }
}
